
package com.android.fitnesscenter.view.client;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties.AuditListResult;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.MapEntity;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class ClientLoginActivity extends BaseActivity {

    @ViewInject(R.id.btn_login)
    public Button btn_login;

    @ViewInject(R.id.txt_forget_password)
    public TextView txt_forget_password;

    @ViewInject(R.id.txt_new_user)
    public TextView txt_new_user;

    @ViewInject(R.id.edtxt_username)
    public EditText edtxt_username;

    @ViewInject(R.id.edtxt_password)
    public EditText edtxt_password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_login_layout);
        ViewUtils.inject(this);
        initView();
    }

    private void initView() {
        initTitleBar(getString(R.string.txt_login));
        ibtn_back.setVisibility(View.GONE);

        String username = mSharePrefsHelper.getString("username", "");
        edtxt_username.setText(username);
        //String password = mSharePrefsHelper.getString("password", "");
        //edtxt_password.setText(password);

        setWifiDialog();
    }

    @OnClick(R.id.btn_login)
    public void btn_login(View view) {
        String username = "";
        if (edtxt_username != null && edtxt_username.getText() != null) {
            username = edtxt_username.getText().toString();
            mSharePrefsHelper.setString("username", username);
        }
        String password = "";
        if (edtxt_password != null && edtxt_password.getText() != null) {
            password = edtxt_password.getText().toString();
            mSharePrefsHelper.setString("password", password);
        }
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            ToastHelper.getInstance().showShortMsg("用户名或密码为空!");
        } else {
            login(FROM_CLIENT_LOGIN_ACTIVITY, username, password);
        }
    }

    @Override
    protected void parseLoginResult(JSONObject jsonObject) throws JSONException {
        if (jsonObject != null && jsonObject.has("user")) {
            JSONObject jsonObject2 = jsonObject.getJSONObject("user");
            MapEntity mapEntity = parseSingleUser(jsonObject2);
            String roleIdStr = mapEntity.getString(AuditListResult.ROLE_ID);
            if (TextUtils.equals(RoleType.USER, roleIdStr)) {
                String token = jsonObject.getString("token");
                mSharePrefsHelper.setString("token", token);
                Intent intent = new Intent(ClientLoginActivity.this, ClientMainActivity.class);
                startActivity(intent);
                finish();
            } else {
                new ViewHelper(ClientLoginActivity.this).showBTNDialog("登陆失败,请重试!");
            }
        }

    }

    @OnClick(R.id.txt_forget_password)
    public void txt_forget_password(View view) {
        Intent intent = new Intent(ClientLoginActivity.this, ClientForgetPasswordActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.txt_new_user)
    public void txt_new_user(View view) {
        Intent intent = new Intent(ClientLoginActivity.this, ClientNewUserActivity.class);
        startActivity(intent);
    }

    @Override
    protected void titlebarMethod(int id) {

    }

}
